package com.sw.c.controller;


import com.sw.c.pojo.entity.Role;
import com.sw.c.pojo.vo.RolePage;
import com.sw.c.service.RoleService;
import com.sw.c.utils.PageResult;
import com.sw.c.utils.ResponseMO;
import com.sw.c.utils.ResponseUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 角色表(Role)表控制层
 *
 * @author makejava
 * @since 2024-04-16 12:31:44
 */
@Api(tags = "Role表相关接口")
@RestController
@RequestMapping("/role")
public class RoleController {
    /**
     * 服务对象
     */
    @Resource
    private RoleService roleService;

    /**
     * 分页查询所有数据
     *
     * @param rolePage 分页对象
     * @return 所有数据
     */
    @RequestMapping(value = "/pageList", method = RequestMethod.POST)
    @ApiOperation(value = "分页列表", response = Role.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "显示第几页", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页显示多少条", required = true),
            @ApiImplicitParam(name = "name", value = "角色名称"),
    })
    public ResponseMO pageList(@RequestBody RolePage rolePage)  {
        if (rolePage == null || rolePage.getPageNum() == null || rolePage.getPageSize() == null) {
            ResponseUtil.error("参数有误");
        }
        PageResult<Role> pageRoleList = roleService.pageList(rolePage);
        return ResponseUtil.successWithData("success",pageRoleList);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @RequestMapping(value = "/getById/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "根据主键ID查询数据", response = Role.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键ID", required = true),
    })
    public ResponseMO getById(@PathVariable Long id) {
        if (id == null) {
            return ResponseUtil.error("参数有误");
        }
    Role role =  roleService.getById(id);
        return ResponseUtil.successWithData("success",role);
    }

    /**
     * 新增数据
     *
     * @param role 实体对象
     * @return 新增结果
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation(value = "新增数据", response = Role.class)
    public ResponseMO add(@RequestBody Role role) {
        if (role == null) {
            ResponseUtil.error("参数有误");
        }
        roleService.save(role);
        return ResponseUtil.success("添加成功");
    }

    /**
     * 修改数据
     *
     * @param role 实体对象
     * @return 修改结果
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ApiOperation(value = "修改数据", response = Role.class)
    public ResponseMO update(@RequestBody Role role) {
        if (role == null) {
            ResponseUtil.error("参数有误");
        }
        roleService.updateById(role);
        return ResponseUtil.success("修改成功");
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除结果
     */
    @RequestMapping(value = "/getById/{id}", method = RequestMethod.POST)
    @ApiOperation(value = "根据主键ID删除数据", response = Role.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键ID", required = true),
    })
    public ResponseMO delete(@PathVariable Integer id) {

        roleService.delRole(id);
        return ResponseUtil.success("删除成功");
    }
}

